/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmediaplayer;

import java.io.File;
import java.io.FileFilter;

/**
 * A FileFilter using to filter the supported audio file extensions of the player
 * @author PHUOCVIET
 */
public class AudioFileFilter
        extends javax.swing.filechooser.FileFilter
        implements FileFilter {

    private boolean allowDirectories;

    /**
     * Create a default MP3FileFilter.  The allowDirectories field will
     * default to false.
     *
     */
    public AudioFileFilter() {
        this(false);
    }

    /**
     * Create an MP3FileFilter.  If allowDirectories is true, then this filter
     * will accept directories as well as mp3 files.  If it is false then
     * only mp3 files will be accepted.
     *
     * @param allowDirectories whether or not to accept directories
     */
    public AudioFileFilter(boolean allowDirectories) {
        this.allowDirectories = allowDirectories;
    }

    /**
     * Determines whether or not the file is an mp3 file.  If the file is
     * a directory, whether or not is accepted depends upon the
     * allowDirectories flag passed to the constructor.
     *
     * @param pathname the file to test
     * @return true if this file or directory should be accepted
     */
    @Override
    public boolean accept(File pathname) {
        boolean retval = false;

        if (pathname.isFile()) {
            String extension = null;
            String s = pathname.getName();
            int i = s.lastIndexOf('.');
            if (i > 0 && i < s.length() - 1) {
                extension = s.substring(i + 1).toLowerCase();
            }
            if (extension != null) {
                if (extension.equals("mp3")
                        || extension.equals("wav")
                        // MPEG-1
                        || extension.equals("mpg")
                        /*
                        || extension.equals("mpeg")
                        || extension.equals("mp1")
                        || extension.equals("mp2")
                        || extension.equals("m1v")
                        || extension.equals("m1a")
                        || extension.equals("m2a")
                        || extension.equals("mpa")
                        || extension.equals("mpv")
                         */
                        // QuickTime
                        /*
                        || extension.equals("aac")
                        || extension.equals("aiff")
                        || extension.equals("pcm")
                        || extension.equals("qt")
                        || extension.equals("mov")
                         */
                        //
                        || extension.equals("avi") //                        || extension.equals("au")
                        //|| extension.equals("mid")
                        ) {
                    retval = true;
                }
            }
        } else if (pathname.isDirectory()) {
            retval = allowDirectories;
        }

        return retval;
    }

    @Override
    public String getDescription() {
        return "Supported audio files";
    }
}
